/* 变量声明 */
let $ = id => document.getElementById(id)
let count = 1
let more = $('more')
let more_ = document.querySelector('.more_')
let span = document.querySelector('#more>span')
let show = document.querySelector('.show')
    /* 点击更多展开菜单功能 */
more.onclick = () => {
        count++
        if (count % 2 === 0) {
            more_.style.display = 'none'
            span.innerHTML = '更多'
        } else {
            more_.style.display = 'flex'
            span.innerHTML = '收起'
            count = 1
        }
    }
    /* 请求数据渲染页面 */
ajax({
    url: 'http://chst.vip:1234/api/getmoneyctrl'

}).then(res => {
    res = res.result
    let arr = res.splice(0, 10)


    show.innerHTML = creat(arr)
    let showAs = document.querySelectorAll('#show>.show>li>a')
    for (let i = 0; i < showAs.length; i++) {

        showAs[i].href = 'indexXiang.html?productid=' + arr[i].productId
    }

    /* 懒加载 */
    /*  window.onscroll = function() {
         let scroT = document.documentElement.scrollTop;
         let scroH = document.documentElement.scrollHeight;
         let clientH = document.documentElement.clientHeight;
         let num = scroH - (scroT + clientH)
         if (num < 100) {
             counts += 2
             if (res.length === 0) {
                 return
             }
             arr = res.splice(0, counts)

             show.innerHTML += creat(arr)

         }
         
         for (let i = 0; i < showAs.length; i++) {
             console.log();
             showAs[i].href = 'indexXiang.html?productid='
         }
     } */
})


/* 封装渲染商品架页面函数 */
function creat(arr) {
    let html = "";
    arr.forEach(item => {
        html += `<li>
<a href="">
   <div class='left'>${item.productImgSm}</div>
   <div class="right">
       <div class="top">
           <p>${item.productName}<span>${item.productPinkage}</span></p>

       </div>
       <div class="bottom">
           <span>${item.productFrom} |${item.productTime}</span>
           <span class="iconfont icon-xiaoxi">${item.productComCount}</span>
       </div>
   </div>
</a>
</li>`
    });
    return html
}


/* 处理菜单栏的a链接 */
let as = document.querySelectorAll('#menu>ul>li>a')
let as2 = document.querySelectorAll('.more_>li>a')
ajax({
        url: 'http://chst.vip:1234/api/getindexmenu'

    }).then(res => {

        res = res.result

        let arr = res.splice(0, 7)

        hrefs(arr, as)
        let arr2 = res.splice(1, 4)
        hrefs(arr2, as2)

    })
    /* 处理a链接函数 */
function hrefs(arr, as) {
    for (let i = 0; i < as.length; i++) {
        as[i].href = arr[i].titlehref
    }

}